<?php
namespace Duobao\Controller;

use Think\Controller\RestController;
use Org\Util\JWT as JWT;

class ControllerBase extends RestController
{
    public function _empty()
    {
        $data = array();
        $data['status'] = 404;
        $data['msg'] = 'action not found';
        
        $this->response($data, 'json');
    }
    
    /**
     * 输出返回数据
     * @access protected
     * @param mixed $data 要返回的数据
     * @param String $type 返回类型 JSON XML
     * @param integer $code HTTP状态
     * @return void
     */
    protected function response($data, $type = 'json', $code = 200)
    {
//         header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
        header('Access-Control-Allow-Origin: *');
        $this->sendHttpStatus($code);
        exit($this->encodeData($data, strtolower($type)));
    }

    /**
     * 通过token获取用户的uid
     */
    protected function getUid($token){
        $token = explode(":",JWT::decode($token,C('SECRET_KEY'),array('HS256')));

        if(time()-$token[1] > 24*3600){
            return 'token Invalid';
        }else{
            return intval($token[0]);
        }


    }

    /**
     * 获取用户注册时候，加密后的验证码token
     */
    public function getCode($mark){
        $token = explode(":",JWT::decode($mark,C('SECRET_KEY'),array('HS256')));

        if(time()-$token[1] > 180){
            return false;
        }else{
            return $token[0];
        }
    }


    /* end of base controller */
}